package ar.edu.itba.pod.legajo48062;

import java.io.IOException;
import java.util.Random;

import org.joda.time.Duration;

import ar.edu.itba.node.Node;
import ar.edu.itba.node.NodeInformation;
import ar.edu.itba.node.api.NodeStatistics;
import ar.edu.itba.node.api.StatisticReports;
import ar.edu.itba.pod.agent.market.Consumer;
import ar.edu.itba.pod.agent.market.Market;
import ar.edu.itba.pod.agent.market.Producer;
import ar.edu.itba.pod.agent.market.Resource;
import ar.edu.itba.pod.time.TimeMapper;
import ar.edu.itba.pod.time.TimeMappers;

public class TPE {	
	public static void main(String[] args) throws IOException { 
		NodeInformation me, remote;
		boolean connected=false;
		boolean exit=false;
		Resource steel = new Resource("Alloy", "Steel");
		int number;
		
		TimeMapper timeMapper = TimeMappers.oneSecondEach(Duration.standardHours(6));
		me = Menu.askLocalInformation();
		Host host = new Host(me,timeMapper);

		do{
			try{
				while(!connected){
					switch(Menu.connectionMenu()){
					case CREATE:
						host.getCluster().createGroup();
						break;
					case CONNECT:
						remote = Menu.askRemoteInformation();
						host.getCluster().connectToGroup(remote.host(), remote.port());
						break;
					}
					connected = host.getCluster().isConnectedToGroup();
					if (!connected){
						System.out.println("No se pudo crear el cluster o conectar al servidor.");
					}
					
				}

				switch(Menu.operationMenu()){
				case STATISTIC:
					System.out.println("COORDINADOR: "+host.getCluster().getCoordinator().id());
					System.out.println("CLUSTER "+host.getCluster().getGroupId());
					new Communication<StatisticReports>(host.getCluster(),Node.STATISTIC_REPORTS){
						@Override
						public void remoteCall(StatisticReports stub, NodeInformation node) throws Exception {
							NodeStatistics statistic = stub.getNodeStatistics();
							System.out.println("NODO "+node.id()+" - CANTIDAD DE AGENTES "+statistic.getNumberOfAgents()+" - AGENTES: "+statistic.getAgentState());
						}
					}.broadcast(null,true,false);					
					break;
				case DISCONNECT:
					host.getCluster().shutdown();
					connected=false;
					exit=true;
					break;
				case START:			
					final Host host2 = host;
					new Thread(){
						public void run() {
							System.out.println("Starting Simulation...");
							try {
								host2.getNode().startAndWait(Duration.standardMinutes(10));
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
							System.out.println("Ending Simulation...");	
						};
					}.start();
					
					break;
				case ADDPRODUCER:
					number = new Random(System.nanoTime()).nextInt()%20;
					host.getNode().add(new Producer("steel alan mine "+number+" " + me.id(), steel, Duration.standardDays(3), 1));
					break;
				case ADDCONSUMER:
					number = new Random(System.nanoTime()).nextInt()%20;
					host.getNode().add(new Consumer("steel alan consumer "+number+" " + me.id(), steel, Duration.standardDays(3), 1));
					break;		
				case ADDMARKET:
					number = new Random(System.nanoTime()).nextInt()%20;
					host.getNode().add(new Market("steel market "+number+" "+me.id(), steel));
					break;					
				case EXIT:
					exit=true;
					break;
				}
			} catch(Exception e){
				//e.printStackTrace();
				System.out.println("ERROR EN EL INGRESO DE DATOS");
			}
		}	while (!exit);

		System.exit(0);
	}
}
